package com.xoa.service.rms.impl;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.rms.RmsRollRoomMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.rms.RmsRollRoom;
import com.xoa.model.users.Users;
import com.xoa.service.rms.RmsRollRoomService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RmsRollRoomServiceImpl implements RmsRollRoomService {

    @Autowired
    private RmsRollRoomMapper rmsRollRoomMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UsersMapper usersMapper;

    /**
     * 作者: 张航宁
     * 日期: 2017/12/22
     * 说明: 保存接口
     */
    @Override
    public ToJson<RmsRollRoom> save(RmsRollRoom rmsRollRoom, HttpServletRequest request) {
        ToJson<RmsRollRoom> json = new ToJson<RmsRollRoom>();
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            rmsRollRoom.setAddUser(user.getUserId());
            rmsRollRoom.setAddTime(new Date());

            rmsRollRoomMapper.insertSelective(rmsRollRoom);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * 作者: 张航宁
     * 日期: 2017/12/22
     * 说明: 更新接口
     */
    @Override
    public ToJson<RmsRollRoom> update(RmsRollRoom rmsRollRoom, HttpServletRequest request) {
        ToJson<RmsRollRoom> json = new ToJson<RmsRollRoom>();
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            rmsRollRoom.setModUser(user.getUserId());
            rmsRollRoom.setModTime(new Date());

            rmsRollRoomMapper.updateByPrimaryKeySelective(rmsRollRoom);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * 作者: 张航宁
     * 日期: 2017/12/22
     * 说明: 删除接口
     */
    @Override
    public ToJson<RmsRollRoom> delete(RmsRollRoom rmsRollRoom, HttpServletRequest request) {
        ToJson<RmsRollRoom> json = new ToJson<RmsRollRoom>();
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            rmsRollRoom.setDelUser(user.getUserId());
            rmsRollRoom.setDelTime(new Date());
            rmsRollRoomMapper.updateByPrimaryKeySelective(rmsRollRoom);
            rmsRollRoomMapper.deleteByPrimaryKey(rmsRollRoom.getRoomId());
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * 作者: 张航宁
     * 日期: 2017/12/22
     * 说明: 查询接口
     */
    @Override
    public ToJson<RmsRollRoom> query(RmsRollRoom rmsRollRoom, PageParams pageParams) {
        ToJson<RmsRollRoom> json = new ToJson<RmsRollRoom>();
        Map<String,Object> map = new HashMap<>();
        map.put("page",pageParams);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
       Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        try{
            Integer deptId = usersMapper.getByUserId(user.getUserId()).getDeptId();
            map.put("deptId",deptId);
            map.put("userId",user.getUserId());
            map.put("userPriv",user.getUserPriv());
            List<RmsRollRoom> rmsRollRooms = rmsRollRoomMapper.queryRooms(map);
            for (RmsRollRoom rmsRolRoom:rmsRollRooms
                 ) {
                rmsRolRoom.setDeptName(departmentMapper.getDeptNameById(rmsRolRoom.getDeptId()));
            }
            json.setTotleNum(pageParams.getTotal());
            json.setObj(rmsRollRooms);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    /**
     * 作者: 张航宁
     * 日期: 2017/12/22
     * 说明: 根据id查询接口
     */
    @Override
    public ToJson<RmsRollRoom> selectById(Integer roomId) {
        ToJson<RmsRollRoom> json = new ToJson<RmsRollRoom>();
        try{
            RmsRollRoom rmsRollRoom = rmsRollRoomMapper.selectById(roomId);
            String[] users = rmsRollRoom.getManageUser().split(",");
            String manageUserName="";
            for (String user:users){
                manageUserName=manageUserName+usersMapper.getUsernameByUserId(user)+",";
            }
            rmsRollRoom.setManageUserName(manageUserName);
            rmsRollRoom.setDeptName(departmentMapper.getDeptNameByDeptId(rmsRollRoom.getDeptId()));
            if(!StringUtils.checkNull(rmsRollRoom.getViewDeptId()) && !"ALL_DEPT".equals(rmsRollRoom.getViewDeptId())){
                String[] split = rmsRollRoom.getViewDeptId().split(",");
                for (String deptId:split) {
                    String deptNameById = departmentMapper.getDeptNameById(Integer.parseInt(deptId));
                    if(!StringUtils.checkNull(rmsRollRoom.getViewDeptName())){
                        rmsRollRoom.setViewDeptName(rmsRollRoom.getViewDeptName()+deptNameById+",");
                    }else{
                        rmsRollRoom.setViewDeptName(deptNameById+",");
                    }
                }
            }else if("ALL_DEPT".equals(rmsRollRoom.getViewDeptId())){
                rmsRollRoom.setViewDeptName("全部部门");
            }
            json.setObject(rmsRollRoom);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    @Override
    public ToJson<RmsRollRoom> selectAll() {
        ToJson<RmsRollRoom> json = new ToJson<RmsRollRoom>();
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            PageParams pageParams = new PageParams();
            Map<String,Object> map = new HashMap<>();
            if(pageParams!=null&&pageParams.getPage()!=null){
                map.put("page",pageParams);
            }
            map.put("deptId",user.getDeptId());
            map.put("userId",user.getUserId());
            map.put("userPriv",user.getUserPriv());
            List<RmsRollRoom> rmsRollRooms = rmsRollRoomMapper.selectAll(map);
            json.setObj(rmsRollRooms);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

}